# -*- coding: utf-8 -*-

from WSModule import WSModule
from WSThread import WSThread
from WSJob import WSJob
from WSResult import WSResult
from WSOption import WSOption
from WSCounter import WSCounter

from DnsFindersBingThread import DnsFindersBingThread
from DnsFindersGoogleThread import DnsFindersGoogleThread
from DnsFindersPagesCounter import DnsFindersPagesCounter
from DnsFindersResult import DnsFindersResult

import os, time
#TODO опцию игнорирования зон, чтоб можно было исключать зоны типа yalta.ua, spb.ru - может найти список таких для стран?

class DnsFinders(WSModule):
	finish = False
	log_path = os.getcwd() + '/results/DNSFinders-result'
	options = {"threads": WSOption("threads", "count thread", 5, False, ['-t','--threads']),
			   "ip": WSOption("ip", "target ip", "5.9.117.26", True, ['-i', '--ip'])}
	pageCounter = DnsFindersPagesCounter()
	
	def run(self):
		self.result = DnsFindersResult(self.options['ip'].value)
		
		workers = []
		for i in range(self.options['threads'].value):
			workers.append(DnsFindersBingThread(self.result, self.options['ip'].value, self.pageCounter))
		self.kernel.createThreads(workers)
		
		while not self.kernel.finished():
			 pass
		
		print("\nBing done, start Google...")
		
		self.result.unique()
		
		counter = WSCounter(10,250)
		workers = []
		job = WSJob()
		for domain in self.result.unique().get_all():
			job.put(domain)
		for i in range(self.options['threads'].value):
			workers.append(DnsFindersGoogleThread(self.result, job, counter))
		self.kernel.createThreads(workers)
		
		while not self.kernel.finished():
			 pass
		
		#results = self.result.get_all()#unique().get_all()
		#print(results)
		#exit(0)
		self.finish = True
		
	def help(self):
		return "Ping module ver 0"
	def finished(self):
		return self.finish
	def getResult(self):
		self.result.validate_result()
		return super().getResult()
		
